package flower.com.a600_xm.db;

import android.content.Context;
import android.util.Log;


import com.j256.ormlite.dao.Dao;

import java.util.List;
import java.util.UUID;

import flower.com.a600_xm.db.dbbean.Life_Shopping;


/**
 * Created by luoliwen on 16/4/28.
 * 完成对用戶数据库的CRUD的操作
 */
public class Z_ShoppingInfoDao {
    private Context context;
    private Dao<Life_Shopping, Integer> userDao;
    private Z_DataBaseHelper helper;

    public Z_ShoppingInfoDao(Context context) {
        this.context = context;
        helper = Z_DataBaseHelper.getInstance(context);
        try {
            userDao = helper.getDao(Life_Shopping.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void addUser(String Name, String Type, String Spec, String Price, String Photo


    ) {
        try {
            Life_Shopping user = new Life_Shopping();
            user.setId(UUID.randomUUID().toString());
            user.setType(Type);
            user.setName(Name);
            user.setSpec(Spec);
            user.setPrice(Price);
            user.setPhoto(Photo);
            userDao.create(user);

        } catch (Exception e) {
            e.printStackTrace();
            Log.e("88888", "" + e);
        }
    }

    List<Life_Shopping> life_shoppings;

    public List<Life_Shopping> query() {
        try {
            life_shoppings = userDao.queryForAll();
//            life_shoppings = userDao.queryBuilder().selectColumns("name").distinct()
//
//                     . query();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return life_shoppings;
    }

    public void deleteUser(Life_Shopping user) {
        try {
            userDao.delete(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public List<Life_Shopping> query(String type) {
        try {
            life_shoppings = userDao.queryBuilder().where().eq("type", type).query();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return life_shoppings;
    }

    Life_Shopping life_shopping;

    public Life_Shopping querys(String name) {
        try {
            life_shopping = userDao.queryBuilder().where().eq("name", name).queryForFirst();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return life_shopping;
    }

}
